#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :get_gjhdq.py
# @Time      :2023/11/8 
# @Author    :CL
# @email     :1037654919@qq.com
import re

import requests
from bs4 import BeautifulSoup
from utils import  mongo_manager
waijiaobu_gjhdq = mongo_manager('waijiaobu_gjhdq',db = 'waijiaobu')
headers = {
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
    "Accept-Language": "zh-CN,zh;q=0.9",
    "Cache-Control": "no-cache",
    "Connection": "keep-alive",
    "Pragma": "no-cache",
    "Referer": "https://www.mfa.gov.cn/web/gjhdq_676201/gj_676203/yz_676205/",
    "Sec-Fetch-Dest": "document",
    "Sec-Fetch-Mode": "navigate",
    "Sec-Fetch-Site": "same-origin",
    "Sec-Fetch-User": "?1",
    "Upgrade-Insecure-Requests": "1",
    "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36",
    "sec-ch-ua": "\"Not.A/Brand\";v=\"8\", \"Chromium\";v=\"114\", \"Google Chrome\";v=\"114\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Linux\""
}
cookies = {
    "HMF_CI": "5a16d8bf0a3ab42047c26c3c3b5b60f423c865ec968964f521c79918fc4dac33f15dfed0818218fe0c7b40f92bff57dfba05612c7105ea346b813bfa47a317cb95",
    "_trs_uv": "lop388ic_469_fpck",
    "arialoadData": "true",
    "ariawapChangeViewPort": "false",
    "_trs_ua_s_1": "lop5aodb_469_dwrc",
    "HMY_JC": "e4ad4adff5c0578cdfcd183cec756d356501f69df4f71aa76bc8c2908dd2c5c304,",
    "HBB_HC": "3f7f75cb8dd610405966b88cf1fb07806a47fd9db7530632e1275a14e53139c64a711717a775cd8cb79695c0c4676ba98b"
}
# 国家和组织
def get_gjhdq(url = "https://www.mfa.gov.cn/web/gjhdq_676201/"):
    response = requests.get(url, headers=headers, cookies=cookies)
    response.encoding = 'utf-8'
    # print(response.text)
    print(response)

    soups = BeautifulSoup(response.text, 'lxml')
    datas = soups.find('div', class_='bd').find_all('li')
    gjhdq_dict ={}
    for data in datas:
        # print(data.get_text())
        # print(data.find('a').get('href'))
        gjhdq_dict[data.get_text()] = data.find('a').get('href').replace('.', '')
    # print(gjhdq_dict)
    return gjhdq_dict
# 国家（地区）
def get_gjhdq_list(url = 'https://www.mfa.gov.cn/web/gjhdq_676201/gj_676203/yz_676205/'):
    response = requests.get(url, headers=headers, cookies=cookies)
    response.encoding = 'utf-8'
    print(response)
    soups = BeautifulSoup(response.text, 'lxml')
    datas = soups.find('div', class_='linkList2').find_all('li')
    lists={}
    for data in datas:
        # print(data.get_text())
        # print(data.find('a').get('href').replace('.', ''))
        lists[data.get_text()] = data.find('a').get('href').replace('./', '')
    return lists
# 国家概况
def get_guojia_data( url = "https://www.mfa.gov.cn/web/gjhdq_676201/gj_676203/yz_676205/1206_676207/1206x0_676209/"):
    headers = {
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
        "Accept-Language": "zh-CN,zh;q=0.9",
        "Cache-Control": "no-cache",
        "Connection": "keep-alive",
        "Pragma": "no-cache",
        "Referer": "https://www.fmprc.gov.cn/web/gjhdq_676201/gj_676203/yz_676205/",
        "Sec-Fetch-Dest": "document",
        "Sec-Fetch-Mode": "navigate",
        "Sec-Fetch-Site": "same-origin",
        "Sec-Fetch-User": "?1",
        "Upgrade-Insecure-Requests": "1",
        "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36",
        "sec-ch-ua": "\"Not.A/Brand\";v=\"8\", \"Chromium\";v=\"114\", \"Google Chrome\";v=\"114\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Linux\""
    }
    cookies = {
        "HMF_CI": "292042a829aa85dd918f16563aa0555eb444efac672ccfb1df0d025db294eb3267dbba0398e0a129e0d4cfbd0c69775ce3085deaad3f08ade6ae6dbd3f8f492cd0",
        "HBB_HC": "1d617d84281737196aa9a1d7b0ad2cca5ecbf0cc5f2bc065f0d8e4a64cdd85d5b123b8a9585f50ee3ddd18404c2fac2e59",
        "_trs_uv": "lop32ilo_469_1nbj",
        "_trs_ua_s_1": "lop32ilo_469_2ua6",
        "arialoadData": "true",
        "ariawapChangeViewPort": "false",
        "HMY_JC": "5d1e3446793309a62b51e41a9e498e39c9615ab1592efee11ba8127475c0bd4108,"
    }
    response = requests.get(url, headers=headers, cookies=cookies)
    response.encoding='utf-8'
    print(response)
    soups = BeautifulSoup(response.text, 'lxml')
    datas = soups.find('div',id='content').find_all('p')
    strs =''
    for data in datas:
        strs +=data.get_text() + '\n'
    return strs.strip()


if __name__ == "__main__":
    print()
    gjhdq_dict = get_gjhdq()
    print(gjhdq_dict)
    for key, value in gjhdq_dict.items():
        print(key)
        url = 'https://www.mfa.gov.cn/web/gjhdq_676201'+value
        lists = get_gjhdq_list(url =url )
        print(lists)
        for  key2, value2 in lists.items():
            try:
                if key2 =='南苏丹':
                    value2 ='nsd_678308/nsdgg_678310/'
                elif key2 in ['世界动物卫生组织', '世界气象组织','地球观测组织','亚洲基础设施投资银行','湄公河委员会','中国—拉共体论坛']:
                    frist = 'gk_'
                    last = int(str(value2).split('_')[-1].replace('/', '')) + 2
                    value2 += frist + str(last) + '/'
                elif key2   in [ '萨赫勒—撒哈拉国家共同体','美洲玻利瓦尔联盟','伊比利亚美洲首脑会议']:
                    frist = 'zzgk_'
                    last = int(str(value2).split('_')[-1].replace('/', '')) + 2
                    value2 += frist + str(last) + '/'
                elif key2 == '新开发银行': #yzkfyh/gk_700180/
                    value2 = 'xkfyh/gk_700180/'
                elif key2 == '亚洲开发银行': #
                    value2 = 'yzkfyh/gk_700180/'
                elif key2 == '不结盟运动':  #
                    value2 = 'bjmyd_682350/zzgk_682352/'
                elif key2 == '澜沧江—湄公河合作':  #
                    value2 = 'lcjmghhz_682662_1/jbqk_682664/'

                elif key2 == '欧盟':  #/1206_679930/1206x0_679932/
                    value2 = '1206_679930/1206x0_679932/'
                elif key2 == '中国—中东欧国家合作':  #/1206_679930/1206x0_679932/
                    value2 = 'zgzdogjhz/1206x0_679932/'
                elif key2 == '北大西洋公约组织':  #/1206_679930/1206x0_679932/
                    value2 = 'bdxygyzz/1206x0_679932/'
                elif key2 == '拉丁美洲社会科学院':  #/1206_679930/1206x0_679932/
                    value2 = 'ldmzshkxy/zgydmgk_683913/'
                elif key =='国际和地区组织':
                    # 1206_676207 lhg_683480/jbqk_683482/
                    frist = 'jbqk_'
                    last = int(str(value2).split('_')[-1].replace('/', '')) + 2
                    value2 += frist + str(last) + '/'
                else :
                    # 1206_676207 lhg_683480/jbqk_683482/
                    frist  =str(value2).split('_')[0] + 'x0_'
                    last  = int(str(value2).split('_')[-1].replace('/','')) + 2
                    value2 +=frist+ str(last) +'/'
                url2 = url + value2
                print(url2)
                data = get_guojia_data(url = url2)
                reslut = {
                    '_id': key2,
                    'nation': key2,
                    'continent': key,
                    'url': url2,
                    'info': data}
                for  p  in  data.split('\n'):
                    pattern = r"【(.*?)】(.*)"
                    match = re.search(pattern, p)
                    if match:
                        extracted_text = match.group(1)
                        # print(extracted_text)
                        reslut[extracted_text] = match.group(2)
                try:
                    waijiaobu_gjhdq.insertOne(reslut)
                except BaseException as e:
                    waijiaobu_gjhdq.updateOne({'_id': key2}, reslut)
            except BaseException as e:
                print(e,key2, value2)
